package com.sunday.volunteer.admin.ui.index;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.sunday.common.event.EventBus;
import com.sunday.common.utils.Constants;
import com.sunday.common.widgets.UIAlertView;
import com.sunday.member.base.BaseActivity;
import com.sunday.member.event.ExitApp;
import com.sunday.member.utils.SharePerferenceUtils;
import com.sunday.volunteer.admin.BaseApp;
import com.sunday.volunteer.admin.R;
import com.sunday.volunteer.admin.adapter.viewpager.BaseFragmentPageAdapter;
import com.sunday.volunteer.admin.entity.Member;
import com.sunday.volunteer.admin.event.UpdateMemberEvent;
import com.sunday.volunteer.admin.fragment.index_tab.IndexTab1Fragment;
import com.sunday.volunteer.admin.fragment.index_tab.IndexTab2Fragment;
import com.sunday.volunteer.admin.ui.login.LoginActivity;
import com.sunday.volunteer.admin.utils.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainDrawerActivity extends BaseActivity {

    @Bind(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    @Bind(R.id.tv_groupInfo)
    TextView tv_groupInfo;
    @Bind(R.id.tv_groupMember)
    TextView tv_groupMember;
    @Bind(R.id.tv_groupPlay)
    TextView tv_groupPlay;
    @Bind(R.id.tv_setting)
    TextView tv_setting;
    @Bind(R.id.tv_loginOut)
    TextView tv_loginOut;
    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.fab)
    FloatingActionButton fab;
    @Bind(R.id.tabLayout)
    TabLayout tabLayout;
    @Bind(R.id.viewPager)
    ViewPager viewPager;
    @Bind(R.id.imageView)
    CircleImageView imageView;
    @Bind(R.id.tv_groName)
    TextView tvGroName;
    @Bind(R.id.ratingStar)
    RatingBar ratingStar;

    private Member member;

    private List<Fragment> list = new ArrayList<>();
    private static String[] type = {"未完成", "已完成"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_drawer);
        ButterKnife.bind(this);
        leftBtn.setVisibility(View.VISIBLE);
        leftBtn.setImageResource(R.mipmap.my);
        titleView.setText("颐高大厦志愿组织");
        member = BaseApp.getInstance().getMember();
        initView();
    }

    private void initView() {
        list.add(new IndexTab1Fragment());
        list.add(new IndexTab2Fragment());
        BaseFragmentPageAdapter pageAdapter
                = new BaseFragmentPageAdapter(getSupportFragmentManager(), list, type);
        viewPager.setAdapter(pageAdapter);
        tabLayout.setupWithViewPager(viewPager);
        if (!TextUtils.isEmpty(member.getLogo())){
            Picasso.with(mContext)
                    .load(member.getLogo())
                    .resize(120,120)
                    .placeholder(R.mipmap.person_default)
                    .error(R.mipmap.person_default)
                    .into(imageView);
        }
        tvGroName.setText(member.getName());
        ratingStar.setRating(member.getScore());
    }

    @OnClick({R.id.tv_groupInfo, R.id.tv_groupMember, R.id.tv_groupPlay,
            R.id.tv_setting, R.id.tv_loginOut,R.id.imageView,R.id.tv_groName,R.id.ratingStar})
    void itemClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.tv_groupInfo:
                intent = new Intent(mContext, GroupInfoActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_groupMember:
                intent = new Intent(mContext, GroupMemberActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_groupPlay:
                intent = new Intent(mContext, GroupApplyActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_setting:
                intent = new Intent(mContext, SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_loginOut:
                showDialog();
                break;
            case R.id.imageView:
                intent = new Intent(mContext, GroupInfoActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_groName:
                intent = new Intent(mContext, GroupInfoActivity.class);
                startActivity(intent);
                break;
            case R.id.ratingStar:
                intent = new Intent(mContext, GroupInfoActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void back(View v) {
        drawerLayout.openDrawer(GravityCompat.START);
    }

    //发布
    @OnClick(R.id.fab)
    public void issue() {
        Intent intent = new Intent(mContext, PublishActivity.class);
        startActivity(intent);
    }

    //双击退出
    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(mContext, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                EventBus.getDefault().post(new ExitApp());
                System.exit(1);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    //退出登陆
    private void showDialog() {
        final UIAlertView delDialog = new UIAlertView(mContext, "温馨提示", "退出登录？",
                "取消", "确定");
        delDialog.setClicklistener(new UIAlertView.ClickListenerInterface() {
                                       @Override
                                       public void doLeft() {
                                           delDialog.dismiss();
                                       }

                                       @Override
                                       public void doRight() {
                                           SharePerferenceUtils.getIns(mContext).removeKey(Constants.IS_LOGIN);
                                           SharePerferenceUtils.getIns(mContext).removeKey("is_loginSelf");
                                           SharePerferenceUtils.getIns(mContext).removeKey("login_time");
                                           SharePerferenceUtils.getIns(mContext).removeKey(Constants.MEMBERID);
                                           EventBus.getDefault().post(new ExitApp());
                                           intent = new Intent(mContext, LoginActivity.class);
                                           startActivity(intent);
                                       }
                                   }
        );
        delDialog.show();
    }


    public void onEvent(UpdateMemberEvent event){
        member = BaseApp.getInstance().getMember();
        if (!TextUtils.isEmpty(member.getLogo())){
            Picasso.with(mContext)
                    .load(member.getLogo())
                    .resize(120,120)
                    .placeholder(R.mipmap.person_default)
                    .error(R.mipmap.person_default)
                    .into(imageView);
        }
        tvGroName.setText(member.getName());
    }

}
